/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file adc_drv.c
 * @brief This file defines adc driver functions
 */

/*******Includes***************************************************************/
#include "adc_reg_access.h"

/*******Definitions************************************************************/

/*******Variables**************************************************************/
/*! @brief Table to save register base addresses pointers for adc instances */
static ADC_Type* s_adcRegBase[CONFIG_ADC_INSTANCE_COUNT] = CONFIG_ADC_REG_BASE_PTRS;

/*! @brief Table to save register base addresses for adc instances */
static uint32_t s_adcRegBaseAddr[CONFIG_ADC_INSTANCE_COUNT] = CONFIG_ADC_BASE_ADDRS;

/*! @brief Table to save adc irq numbers */
static const IRQn_Type s_adcIrqId[CONFIG_ADC_INSTANCE_COUNT] = CONFIG_ADC_IRQS;

/*! @brief Table to save adc module names */
static module_names_t s_adcModuleName[CONFIG_ADC_INSTANCE_COUNT] = CONFIG_ADC_MODULE_NAMES;

/*! @brief Table to save adc clock names */
static clk_names_t s_adcClkName[CONFIG_ADC_INSTANCE_COUNT] = CONFIG_ADC_CLOCK_NAMES;

/*! @brief Table to save adc interrupt callbacks */
static adc_callback_t s_adcCallback[CONFIG_ADC_INSTANCE_COUNT] = {NULL};

/*! @brief Table to save adc interrupt status */
static adc_int_state_t s_adcIntStatus[CONFIG_ADC_INSTANCE_COUNT] = {0U};

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/

/*******************************************************************************
 *** Function Name : ADC_Init
 *** Description   : Configure the ADC conversion related functions
 ******************************************************************************/
void ADC_Init(uint32_t instance, adc_conversion_config_t* config)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(config != NULL);

    ADC_Type* regBase = s_adcRegBase[instance];

    /* Select adc function clock source */
    (void)CLOCK_SelectModuleClockSource(s_adcClkName[instance], (uint16_t)(config->srcClk));

    /* Enable adc module clock */
    CLOCK_SetModuleBusClockEnable(s_adcModuleName[instance], true);
    /* Set soft reset inactive of adc module */
    CLOCK_SetModuleSoftResetState(s_adcModuleName[instance], RESET_INACTIVE);

    ADC_REG_SetSampleTiming(s_adcRegBaseAddr[instance]);
    ADC_REG_SetDataResolution(regBase, config->resolution);
    ADC_REG_SetAverageTimes(regBase, config->averageTimes);
    ADC_REG_SetAverageEn(regBase, config->averageEn);
    ADC_REG_SetVrefP(regBase, config->vrefpSel);
#if (CONFIG_ADC_SUPPORT_VREFL == 1U)
    ADC_REG_SetVrefN(regBase, config->vrefnSel);
#endif
    ADC_REG_SetDMAMode(regBase, config->dmaMode);

    if (config->normalSequence != NULL) {
        ADC_InitNormalSequence(instance, config->normalSequence);
    }

    if (config->prioritySequence != NULL) {
        ADC_InitPrioritySequence(instance, config->prioritySequence);
    }

#if (CONFIG_FUNCTION_SAFE == 0U)
    ADC_SetBufferSel(instance, config->bufferSel);
#endif

    if (config->intEn == true) {
        NVIC_EnableIRQ(s_adcIrqId[instance]);
    } else {
        NVIC_DisableIRQ(s_adcIrqId[instance]);
    }
    s_adcCallback[instance] = config->callback;

    ADC_REG_SetADCEn(regBase, config->adcEn);
}

/*******************************************************************************
 *** Function Name : ADC_GetDefaultConfig
 *** Description   : Get ADC default configuration
 ******************************************************************************/
void ADC_GetDefaultConfig(adc_conversion_config_t* config)
{
    ASSERT_PARAM(config != NULL);

    config->adcEn = false;
    config->intEn = false;
    config->resolution = ADC_RESOLUTION_12BIT;
    config->averageEn = false;
    config->averageTimes = ADC_AVERAGE_4;
    config->dmaMode = ADC_DMA_DISABLE;
    config->vrefpSel = ADC_VDDA;
#if (CONFIG_ADC_SUPPORT_VREFL == 1U)
    config->vrefnSel = ADC_GND;
#endif
    config->callback = NULL;

    config->normalSequence->triggerType = ADC_SOFTWARE_TRIGGER;
    config->normalSequence->sequenceLen = 0U;
    config->normalSequence->mode = ADC_SINGLE_MODE;
    config->normalSequence->neosIntEn = true;

    config->prioritySequence->triggerType = ADC_SOFTWARE_TRIGGER;
    config->prioritySequence->sequenceLen = 0U;
    config->prioritySequence->mode = ADC_SCAN_MODE;
    config->prioritySequence->peosIntEn = true;

}

/*******************************************************************************
 *** Function Name : ADC_InitNormalSequence
 *** Description   : Configure the ADC normal channel sequence conversion
 ***                 related functions
 ******************************************************************************/
void ADC_InitNormalSequence(uint32_t instance, const adc_normal_sequence_config_t* config)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(config != NULL);
    ASSERT_PARAM(config->sequenceLen < CONFIG_ADC_MAX_NORMAL_SEQUENCE_LEN);

    ADC_Type* regBase = s_adcRegBase[instance];
    uint32_t i = 0U;

    ADC_REG_SetNormalChTriggerMode(regBase, config->triggerType);
    ADC_REG_SetNormalChMode(regBase, config->mode);
    ADC_REG_SetNEOSIntEn(regBase, config->neosIntEn);
    ADC_REG_SetNormalChSequenceLen(regBase, config->sequenceLen);

    for (i = 0U; i <= config->sequenceLen; i++) {
        ASSERT_PARAM(&(config->chConfig[i]) != NULL);

        ADC_InitNormalChannel(instance, (adc_normal_channel_t)i, &(config->chConfig[i]));
    }
}

/*******************************************************************************
 *** Function Name : ADC_InitNormalChannel
 *** Description   : Configure the ADC normal channel conversion related functions
 ******************************************************************************/
void ADC_InitNormalChannel(uint32_t instance, adc_normal_channel_t channel,
                           const adc_normal_ch_config_t* config)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(config != NULL);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetNormalChInputSel(regBase, (adc_normal_channel_t)channel, config->channel);
    ADC_REG_SetNormalChSampleTime(regBase, (adc_normal_channel_t)channel, config->sampleTime);
    ADC_REG_SetNormalChEOCIntEn(regBase, (adc_normal_channel_t)channel, config->neocIntEn);
    ADC_REG_SetNormalChAwdSel(regBase, (adc_normal_channel_t)channel, config->awdSel);
    ADC_REG_SetNormalChAwdEn(regBase, (adc_normal_channel_t)channel, config->awdEn);
    ADC_REG_SetNormalChAwdTrigEn(regBase, (adc_normal_channel_t)channel, config->awdTrigEn);
    ADC_REG_SetNormalChAwdIntEn(regBase, (adc_normal_channel_t)channel, config->awdIntEn);
}

/*******************************************************************************
 *** Function Name : ADC_SetNormalChSequenceLen
 *** Description   : Configure the ADC normal channel sequence length,
 ***                 0 indicates that the actual sequence length is 1
 ******************************************************************************/
void ADC_SetNormalChSequenceLen(uint32_t instance, uint8_t len)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(len < CONFIG_ADC_MAX_NORMAL_SEQUENCE_LEN);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetNormalChSequenceLen(regBase, len);
}

/*******************************************************************************
 *** Function Name : ADC_InitPrioritySequence
 *** Description   : Configure the ADC priority channel sequence conversion
 ***                 related functions
 ******************************************************************************/
void ADC_InitPrioritySequence(uint32_t instance, const adc_priority_sequence_config_t* config)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(config != NULL);
    ASSERT_PARAM(config->sequenceLen < CONFIG_ADC_MAX_PRIORITY_SEQUENCE_LEN);

    ADC_Type* regBase = s_adcRegBase[instance];
    uint32_t i = 0U;

    ADC_REG_SetPriorityChTriggerMode(regBase, config->triggerType);
    ADC_REG_SetPriorityChMode(regBase, config->mode);
    ADC_REG_SetPEOSIntEn(regBase, config->peosIntEn);
    ADC_REG_SetPriorityChSequenceLen(regBase, config->sequenceLen);

    for (i = 0U; i <= config->sequenceLen; i++) {
        ASSERT_PARAM(&(config->chConfig[i]) != NULL);

        ADC_InitPriorityChannel(instance, (adc_priority_channel_t)i, &(config->chConfig[i]));
    }
}

/*******************************************************************************
 *** Function Name : ADC_InitPriorityChannel
 *** Description   : Configure the ADC priority channel conversion related functions
 ******************************************************************************/
void ADC_InitPriorityChannel(uint32_t instance, adc_priority_channel_t channel,
                             const adc_priority_ch_config_t* config)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(config != NULL);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetPriorityChInputSel(regBase, (adc_priority_channel_t)channel, config->channel);
    ADC_REG_SetPriorityChSampleTime(regBase, (adc_priority_channel_t)channel, config->sampleTime);
    ADC_REG_SetPriorityChEOCIntEn(regBase, (adc_priority_channel_t)channel, config->peocIntEn);
    ADC_REG_SetPriorityChAwdSel(regBase, (adc_priority_channel_t)channel, config->awdSel);
    ADC_REG_SetPriorityChAwdEn(regBase, (adc_priority_channel_t)channel, config->awdEn);
    ADC_REG_SetPriorityChAwdTrigEn(regBase, (adc_priority_channel_t)channel, config->awdTrigEn);
    ADC_REG_SetPriorityChAwdIntEn(regBase, (adc_priority_channel_t)channel, config->awdIntEn);
    ADC_REG_SetPriorityChOffsetVal(regBase, (adc_priority_channel_t)channel, config->offset);
}

/*******************************************************************************
 *** Function Name : ADC_SetPriorityChSequenceLen
 *** Description   : Configure the ADC priority channel sequence length,
 ***                 0 indicates that the actual sequence length is 1
 ******************************************************************************/
void ADC_SetPriorityChSequenceLen(uint32_t instance, uint8_t len)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(len < CONFIG_ADC_MAX_PRIORITY_SEQUENCE_LEN);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetPriorityChSequenceLen(regBase, len);
}

/*******************************************************************************
 *** Function Name : ADC_InitAnalogWatchdog
 *** Description   : Configure the ADC analog watchdog related functions
 ******************************************************************************/
void ADC_InitAnalogWatchdog(uint32_t instance, const  adc_analog_watchdog_config_t* config)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);
    ASSERT_PARAM(config != NULL);

    ADC_Type* regBase = s_adcRegBase[instance];
    uint32_t i = 0U;
    adc_analog_watchdog_t awdChannel = ADC_ANALOG_WATCHDOG0;

    for (i = 0U; i < config->awdNum; i++) {
        ASSERT_PARAM(&(config->awdChConfig[i]) != NULL);

        awdChannel = config->awdChConfig[i].awdChannel;
        ADC_REG_SetAwdHighThreshold(regBase, awdChannel, config->awdChConfig[i].awdHighThreshold);
        ADC_REG_SetAwdLowThreshold(regBase, awdChannel, config->awdChConfig[i].awdLowThreshold);
    }
}

/*******************************************************************************
 *** Function Name : ADC_SetAwdHighThreshold
 *** Description   : Configure the analog watchdog high threshold
 ******************************************************************************/
void ADC_SetAwdHighThreshold(uint32_t instance, adc_analog_watchdog_t awdCh, uint16_t threshold)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetAwdHighThreshold(regBase, awdCh, threshold);
}

/*******************************************************************************
 *** Function Name : ADC_SetAwdLowThreshold
 *** Description   : Configure the analog watchdog low threshold
 ******************************************************************************/
void ADC_SetAwdLowThreshold(uint32_t instance, adc_analog_watchdog_t awdCh, uint16_t threshold)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetAwdLowThreshold(regBase, awdCh, threshold);
}

/*******************************************************************************
 *** Function Name : ADC_SetDataResolution
 *** Description   : Configure the ADC conversion resolution
 ******************************************************************************/
void ADC_SetDataResolution(uint32_t instance, adc_resolution_t resolution)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetDataResolution(regBase, resolution);
}

/*******************************************************************************
 *** Function Name : ADC_SetSampleAverage
 *** Description   : Configure the ADC sample average function
 ******************************************************************************/
void ADC_SetSampleAverage(uint32_t instance, adc_average_t averageTimes, bool averageEn)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetAverageTimes(regBase, averageTimes);
    ADC_REG_SetAverageEn(regBase, averageEn);
}

#if (CONFIG_FUNCTION_SAFE == 0U)
/*******************************************************************************
 *** Function Name : ADC_SetBufferSel
 *** Description   : Configure the ADC buffer select
 ******************************************************************************/
void ADC_SetBufferSel(uint32_t instance, adc_buffer_sel_t bufferSel)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    if (bufferSel == ADC_BUFFER_DISABLE) {
        ADC_REG_SetBufferEn(regBase, false);
    } else {
        ADC_REG_SetBufferEn(regBase, true);
        ADC_REG_BufferSel(regBase, bufferSel);
    }

}
#endif

/*******************************************************************************
 *** Function Name : ADC_GetNormalChResult
 *** Description   : Get ADC normal channel conversion result
 ******************************************************************************/
uint16_t ADC_GetNormalChResult(uint32_t instance, adc_normal_channel_t channel)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetNormalChResult(regBase, channel);
}

/*******************************************************************************
 *** Function Name : ADC_GetPriorityChResult
 *** Description   : Get ADC priority channel conversion result
 ******************************************************************************/
int16_t ADC_GetPriorityChResult(uint32_t instance, adc_priority_channel_t channel)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetPriorityChResult(regBase, channel);
}

/*******************************************************************************
 *** Function Name : ADC_SetCalibrationEn
 *** Description   : Enable/Disable ADC calibration function
 ******************************************************************************/
void ADC_SetCalibrationEn(uint32_t instance, bool calibrationEn)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetCalibrationEn(regBase, calibrationEn);
}

/*******************************************************************************
 *** Function Name : ADC_StopConversion
 *** Description   : Stop ADC current conversion
 ******************************************************************************/
void ADC_StopConversion(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_StopConversion(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_SetNormalChSoftwareTrigger
 *** Description   : Set software trigger for normal channel
 ******************************************************************************/
void ADC_SetNormalChSoftwareTrigger(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetNormalChSoftwareTrigger(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_SetPriorityChSoftwareTrigger
 *** Description   : Set software trigger for priority channel
 ******************************************************************************/
void ADC_SetPriorityChSoftwareTrigger(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_SetPriorityChSoftwareTrigger(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_GetIdleStatus
 *** Description   : Get ADC idle status
 ******************************************************************************/
bool ADC_GetIdleStatus(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetIdleStatus(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_GetAllEOSFlag
 *** Description   : Get end of sequence conversion flag for normal channel
 ***                 and priority channel
 ******************************************************************************/
uint8_t ADC_GetAllEOSFlag(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetAllEOSFlag(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_ClrAllEOSFlag
 *** Description   : Clear end of sequence conversion flag for normal channel
 ***                 and priority channel
 ******************************************************************************/
void ADC_ClrAllEOSFlag(uint32_t instance, uint8_t eosMsk)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_ClrAllEOSFlag(regBase, eosMsk);
}

/*******************************************************************************
 *** Function Name : ADC_GetAllNEOCFlag
 *** Description   : Get all end of normal channel conversion flag
 ******************************************************************************/
uint32_t ADC_GetAllNEOCFlag(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetAllNEOCFlag(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_ClrAllNEOCFlag
 *** Description   : Clear all end of normal channel conversion flag
 ******************************************************************************/
void ADC_ClrAllNEOCFlag(uint32_t instance, uint32_t neocMsk)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_ClrAllNEOCFlag(regBase, neocMsk);
}

/*******************************************************************************
 *** Function Name : ADC_GetAllPEOCFlag
 *** Description   : Get all end of priority channel conversion flag
 ******************************************************************************/
uint8_t ADC_GetAllPEOCFlag(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetAllPEOCFlag(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_ClrAllPEOCFlag
 *** Description   : Clear all end of priority channel conversion flag
 ******************************************************************************/
void ADC_ClrAllPEOCFlag(uint32_t instance, uint8_t peocMsk)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_ClrAllPEOCFlag(regBase, peocMsk);
}

/*******************************************************************************
 *** Function Name : ADC_GetAllNAwdFlag
 *** Description   : Get all analog watchdog event flag for normal channel
 ******************************************************************************/
uint32_t ADC_GetAllNAwdFlag(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetAllNAwdFlag(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_ClrAllNAwdFlag
 *** Description   : Clear all analog watchdog event flag for normal channel
 ******************************************************************************/
void ADC_ClrAllNAwdFlag(uint32_t instance, uint32_t nawdMsk)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_ClrAllNAwdFlag(regBase, nawdMsk);
}

/*******************************************************************************
 *** Function Name : ADC_GetAllPAwdFlag
 *** Description   : Get all analog watchdog event flag for priority channel
 ******************************************************************************/
uint8_t ADC_GetAllPAwdFlag(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    return ADC_REG_GetAllPAwdFlag(regBase);
}

/*******************************************************************************
 *** Function Name : ADC_ClrAllPAwdFlag
 *** Description   : Clear all analog watchdog event flag for priority channel
 ******************************************************************************/
void ADC_ClrAllPAwdFlag(uint32_t instance, uint8_t pawdMsk)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    ADC_Type* regBase = s_adcRegBase[instance];

    ADC_REG_ClrAllPAwdFlag(regBase, pawdMsk);
}

/*******************************************************************************
 *** Function Name : ADC_Deinit
 *** Description   : Deinit ADC
 ******************************************************************************/
void ADC_Deinit(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_ADC_INSTANCE_COUNT);

    /* Set soft reset active of adc module */
    CLOCK_SetModuleSoftResetState(s_adcModuleName[instance], RESET_ACTIVE);
    /* Disable adc module clock */
    CLOCK_SetModuleBusClockEnable(s_adcModuleName[instance], false);

    NVIC_DisableIRQ(s_adcIrqId[instance]);
    NVIC_ClearPendingIRQ(s_adcIrqId[instance]);
}

/*******************************************************************************
 *** Function Name : ADC_IRQHandlerProcess
 *** Description   : ADC interrupt interrupt implement
 ******************************************************************************/
void ADC_IRQHandlerProcess(uint32_t instance)
{
    adc_int_state_t* intStatus = {NULL};

    intStatus = &s_adcIntStatus[instance];

    if (s_adcCallback[instance] != NULL) {
        s_adcCallback[instance](instance, (void*)intStatus);
    } else {
        intStatus->allEosFlag = ADC_GetAllEOSFlag(instance);
        if (intStatus->allEosFlag) {
            ADC_ClrAllEOSFlag(instance, intStatus->allEosFlag);
        }

        intStatus->allNeocFlag = ADC_GetAllNEOCFlag(instance);
        if (intStatus->allNeocFlag) {
            ADC_ClrAllNEOCFlag(instance, intStatus->allNeocFlag);
        }

        intStatus->allPeocFlag = ADC_GetAllPEOCFlag(instance);
        if (intStatus->allPeocFlag) {
            ADC_ClrAllPEOCFlag(instance, intStatus->allPeocFlag);
        }

        intStatus->allNawdFlag = ADC_GetAllNAwdFlag(instance);
        if (intStatus->allNawdFlag) {
            ADC_ClrAllNAwdFlag(instance, intStatus->allNawdFlag);
        }

        intStatus->allPawdFlag = ADC_GetAllPAwdFlag(instance);
        if (intStatus->allPawdFlag) {
            ADC_ClrAllPAwdFlag(instance, intStatus->allPawdFlag);
        }
    }
}

/*******EOF********************************************************************/

